package com.example.demo.controller;

import com.example.demo.entity.BookFeedback;
import com.example.demo.mapper.BookFeedbackMapper;
import com.example.demo.utils.PagingResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/book-feedback") // 控制器的前缀地址
public class BookFeedbackController {
    @Autowired
    BookFeedbackMapper bookFeedbackMapper;

    /**
     * 获取分页的图书反馈数据
     * @param limit 每页数量
     * @param offset 偏移量
     * @param field 搜索字段
     * @param code 搜索关键词
     * @return 分页结果
     */
    @GetMapping("/list")
    public PagingResult<BookFeedback> select(
            @RequestParam(required = false, name = "limit", defaultValue = "10") int limit,
            @RequestParam(required = false, name = "offset", defaultValue = "0") int offset,
            @RequestParam(required = false, name = "field") String field,
            @RequestParam(required = false, name = "code") String code
    ) {
        List<BookFeedback> bookFeedbackList;
        int total;
        
        if (field != null && code != null && !code.trim().isEmpty()) {
            bookFeedbackList = bookFeedbackMapper.findByPageAndSearch(offset, limit, field, code);
            total = bookFeedbackMapper.countBySearch(field, code);
        } else {
            bookFeedbackList = bookFeedbackMapper.findByPage(offset, limit);
            total = bookFeedbackMapper.count();
        }
        
        return new PagingResult<BookFeedback>(bookFeedbackList, total);
    }

    /**
     * 根据ID获取图书反馈
     * @param id 反馈ID
     * @return 图书反馈对象
     */
    @GetMapping("/{id}")
    public BookFeedback getById(@PathVariable("id") int id) {
        return bookFeedbackMapper.findById(id);
    }

    /**
     * 根据会员ID获取图书反馈列表
     * @param membId 会员ID
     * @return 图书反馈列表
     */
    @GetMapping("/member/{membId}")
    public List<BookFeedback> getByMembId(@PathVariable("membId") int membId) {
        return bookFeedbackMapper.findByMembId(membId);
    }

    /**
     * 根据图书ID获取图书反馈列表
     * @param bookId 图书ID
     * @return 图书反馈列表
     */
    @GetMapping("/book/{bookId}")
    public List<BookFeedback> getByBookId(@PathVariable("bookId") int bookId) {
        return bookFeedbackMapper.findByBookId(bookId);
    }

    /**
     * 添加图书反馈
     * @param bookFeedback 图书反馈对象
     * @return 影响行数
     */
    @PostMapping("/add")
    public int add(@RequestBody BookFeedback bookFeedback) {
        return bookFeedbackMapper.insert(bookFeedback);
    }

    /**
     * 更新图书反馈
     * @param bookFeedback 图书反馈对象
     * @return 影响行数
     */
    @PutMapping("/update")
    public int update(@RequestBody BookFeedback bookFeedback) {
        return bookFeedbackMapper.update(bookFeedback);
    }

    /**
     * 删除图书反馈
     * @param id 反馈ID
     * @return 影响行数
     */
    @DeleteMapping("/delete/{id}")
    public int delete(@PathVariable("id") int id) {
        return bookFeedbackMapper.delete(id);
    }
}